package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.dto.YddAdvimgPageDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.mapper.YddAdvposMapper;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddAdvimgService;
import com.youdianda.mapper.YddAdvimgMapper;
import com.youdianda.service.YddAdvposService;
import com.youdianda.vo.YddAdvimgVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author xliu
* @description 针对表【ydd_advimg】的数据库操作Service实现
* @createDate 2023-05-14 16:01:34
*/
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg>
    implements YddAdvimgService  {
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;
    @Autowired
    private YddAdvposMapper yddAdvposMapper;

    @Override
    public Result rowInfo(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgMapper.selectById(id);
        return Result.success(yddAdvimg);
    }
/*修改广告图*/
    @Override
    public Result updateAdvimg(YddAdvimg yddAdvimg) {
        YddAdvimg yddAdvimg1=new YddAdvimg();
        BeanUtils.copyProperties(yddAdvimg,yddAdvimg1);
        yddAdvimgService.updateById(yddAdvimg1);
        return Result.success();
    }



    //广告图列表-分页查询
    @Override
    public Result list(YddAdvimgPageDTO yddAdvimgPageDTO) {
        //非空判断
        if (yddAdvimgPageDTO.getPage() == null) {
            //设置当前页默认值
            yddAdvimgPageDTO.setPage("1");
        }
        if (yddAdvimgPageDTO.getLimit() == null) {
            //设置总页默认值
            yddAdvimgPageDTO.setLimit("10");
        }

        //创建IPage分页对象,设置分页参数
        IPage<YddAdvimg> page = new Page<>(Integer.valueOf(yddAdvimgPageDTO.getPage()), Integer.valueOf(yddAdvimgPageDTO.getLimit()));

        //执行分页条件查询
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<YddAdvimg>();
        if (yddAdvimgPageDTO.getAdvimgdesc() != null) {
            wrapper.like(YddAdvimg::getAdvimgdesc, yddAdvimgPageDTO.getAdvimgdesc());
        }
        if (yddAdvimgPageDTO.getAdvimgpos() != null) {
            wrapper.like(YddAdvimg::getAdvimgpos, yddAdvimgPageDTO.getAdvimgpos());
        }

        List<YddAdvimgVO> list = new ArrayList<>();
        IPage<YddAdvimg> page2 = yddAdvimgMapper.selectPage(page, wrapper);
        for (YddAdvimg record : page2.getRecords()) {
            YddAdvimgVO yddAdvimg = new YddAdvimgVO();
            YddAdvpos yddAdvpos = yddAdvposMapper.selectById(record.getAdvimgpos());
            if (yddAdvpos != null){
                BeanUtils.copyProperties(record,yddAdvimg);
                String name = yddAdvpos.getAdvposname() == null? "":yddAdvpos.getAdvposname();
                yddAdvimg.setAdvposname(name);
            }
            if (yddAdvimg.getAdvposname() != null) {
                list.add(yddAdvimg);
            }
        }
        PageResult pageResult = new PageResult();
        pageResult.setCount((int) page2.getTotal());
        pageResult.setTotalPages((int) page2.getPages());
        pageResult.setPageSize((int) page2.getSize());
        pageResult.setCurrentPage((int) page2.getCurrent());
        pageResult.setData(list);

        return Result.success(pageResult);
    }}





